#-*-coding:utf-8-*-
import os
import sys
import csv
from PySide2.QtWidgets import *
from PySide2.QtCore import *
from PySide2.QtGui import *

class Setup(QWidget):

    def __init__(self):
        super(Setup,self).__init__()
        self.setStyleSheet('background-color:rgb(75,75,75);color:rgb(200,200,200)}'
                           )
        #全局变量
        config_path=os.path.join(os.path.dirname(os.path.dirname(__file__)),'config')
        self.USER_INFO_PATH = os.path.join(config_path,'user.info')
        self.password_same=False

        self.setWindowTitle('注册')
        self.mainLayout=QVBoxLayout()
        self.mainLayout.setContentsMargins(15,30,15,30)
        self.mainLayout.setSpacing(20)

        self.setup_logo_label=QLabel('用户信息')
        self.setup_logo_label.setAlignment(Qt.AlignCenter)
        self.username_layout=QHBoxLayout()
        self.username_label=QLabel('登录账号:')
        self.username_edit=QLineEdit()
        self.username_layout.addWidget(self.username_label)
        self.username_layout.addWidget(self.username_edit)

        self.password_layout=QHBoxLayout()
        self.password_label=QLabel('设定密码:')
        self.password_edit=QLineEdit()
        self.password_edit.setEchoMode(QLineEdit.Password)
        self.password_layout.addWidget(self.password_label)
        self.password_layout.addWidget(self.password_edit)

        self.repeat_password_layout=QHBoxLayout()
        self.repeat_password_label=QLabel('重复密码:')
        self.repeat_password_edit=QLineEdit()
        self.repeat_password_edit.textChanged.connect(self.__passwordLabelColor)
        self.repeat_password_edit.setEchoMode(QLineEdit.Password)
        self.repeat_password_layout.addWidget(self.repeat_password_label)
        self.repeat_password_layout.addWidget(self.repeat_password_edit)

        self.showname_layout=QHBoxLayout()
        self.showname_label=QLabel('显示昵称:')
        self.showname_edit=QLineEdit()
        self.showname_layout.addWidget(self.showname_label)
        self.showname_layout.addWidget(self.showname_edit)

        self.btn_layout=QHBoxLayout()
        self.OK_btn=QPushButton('确认')
        self.OK_btn.clicked.connect(self.user_setup)
        self.NO_btn=QPushButton('取消')
        self.NO_btn.clicked.connect(self.__close)
        self.btn_layout.addWidget(self.OK_btn)
        self.btn_layout.addWidget(self.NO_btn)

        self.mainLayout.addWidget(self.setup_logo_label)
        self.mainLayout.addLayout(self.username_layout)
        self.mainLayout.addLayout(self.password_layout)
        self.mainLayout.addLayout(self.repeat_password_layout)
        self.mainLayout.addLayout(self.showname_layout)
        self.mainLayout.addLayout(self.btn_layout)

        self.setLayout(self.mainLayout)

    def __passwordLabelColor(self):
        '''
        实时检测二次密码是否一致
        :return:
        '''
        origin_password= self.password_edit.text()
        current_password=self.repeat_password_edit.text()
        if origin_password and current_password:
            if current_password !=origin_password:
                self.repeat_password_label.setText("密码不同:")
                self.repeat_password_label.setStyleSheet("color : rgb(255,0,0)")
                self.password_same=False
            else:
                self.repeat_password_label.setText("重复密码:")
                self.repeat_password_label.setStyleSheet("color : rgb(200,200,200)")
                self.password_same=True

    def user_setup(self):
        '''
        注册
        :return:
        '''
        if self.is_user():
            QMessageBox.information(self, '提示', '该用户已经注册')
        else:
            username=self.username_edit.text()
            password=self.password_edit.text()
            showname=self.showname_edit.text()
            if not username or not showname:
                QMessageBox.information(self, '提示', '请输入完整信息')
            else:
                if self.password_same:
                    user_info=[username,password,showname]
                    with open(self.USER_INFO_PATH,'a',newline='',encoding='utf-8') as f:
                        csv_writer=csv.writer(f,dialect='excel')
                        csv_writer.writerow(user_info)

                    QMessageBox.information(self, '恭喜', '注册成功')
                    self.close()
                else:
                    QMessageBox.information(self,'提示','密码不一致')

    def is_user(self):
        is_user=False
        if not os.path.exists(self.USER_INFO_PATH):
            is_user= False
        else:
            with open(self.USER_INFO_PATH,'r',encoding='utf-8') as r:
                read=csv.reader(r)
                for s in read:
                    if s[0]==self.username_edit.text():
                        is_user= True
                        break
                    else:
                        is_user= False

        return  is_user
    def __close(self):
        self.close()

    def __test(self):
        print(self.USER_INFO_PATH)

if __name__ == '__main__':
    app=QApplication()
    s=Setup()
    s.show()
    sys.exit(app.exec_())
